package jm.algorithm.dp;

/**
 * @Description 最长上升子序列
 * @date 2022/5/23 21:17
 */
public class LIS {
    public static void main(String[] args) {
        int[] arr = {10,2,2,5,1,7,101,18};
        System.out.println(lis(arr));
    }

    /**
     * dp 实现最长上升子序列
     * @param nums 整体序列
     * @return
     */
    static int lis(int[] nums){
        if (nums == null || nums.length == 0) return 0;
        int[] dp = new int[nums.length];
        // 初始化状态值
        int max = dp[0] = 1;
        for (int i = 0; i < dp.length; i++) {
            // 初始化状态值
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[i] <=  nums[j]) continue;
                dp[i] = Math.max(dp[i], dp[j] + 1);
            }
            max = Math.max(dp[i], max);
        }
        return max;
    }
}
